import { Exclude } from 'class-transformer';
import { IsOptional } from 'class-validator';
import {
  BeforeInsert,
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

type SpecsType = {
  specsId: number
  specsName: string;
  specsUnit: string;
  price: number;
  promotionPrice: number;
  inventory: number;
};

@Entity('promotion', { schema: 'shop' })
export class Promotion {
  constructor(partial: Partial<Promotion>) {
    Object.assign(this, partial);
  }

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'category_id' })
  categoryId: number;

  @Column({ name: 'goods_id' })
  goodsId: number;

  @Column({ length: 50 })
  title: string;

  @Column({ length: 500 })
  desc: string;

  @Column({ name: 'advertise_pic', type: 'json' })
  advertisePic: Array<{ url: string }>;

  @Column({ name: 'specs_list', type: 'json' })
  specsList: SpecsType[];

  @Column({ name: 'file_list', type: 'json' })
  @IsOptional()
  fileList: Array<{ url: string }>;

  @Column({ type: 'json' })
  @IsOptional()
  detail: Array<{ url: string }>;

  @Column({ name: 'is_delete', type: 'tinyint', default: 1, select: false })
  @Exclude()
  isDelete: number;

  @CreateDateColumn({ name: 'create_time' })
  createTime: Date;

  @UpdateDateColumn({ name: 'modify_time' })
  modifyTime: Date;

  // // 数据保存之前，先进行 string 格式化
  // @BeforeInsert()
  // async encryptPassword() {
  //   if (this.fileList) {
  //     this.fileList = JSON.stringify(this.fileList);
  //   }
  // }
}
